package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ImageView;

public class MoveView extends ImageView {

    private float chaX;
    private float chaY;
    private float downX;
    private float downY;
    private float mSouch;
    private float mLastX;
    private float mLastY;
    private Context mContext;

    public MoveView(Context context) {
        this(context,null);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mSouch = ViewConfiguration.get(this.mContext).getScaledTouchSlop();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float tempX = event.getX();
        float tempY = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = tempX;
                downY = tempY;
                chaX = getX() - event.getRawX();
                chaY = getY() - event.getRawY();
            case MotionEvent.ACTION_MOVE:
                float moveX = tempX - downX;
                float moveY = tempY - downY;
//                Log.i("test","getx = "+getX() +" movex = "+moveX+ " gety = "+getY() +" movey = "+moveY +" /n bbbbbbbbbbottom = "+get);

                if(Math.abs(moveX) > mSouch || Math.abs(moveY) > mSouch){
                    this.setX(event.getRawX() + chaX);
                    this.setY(event.getRawY() + chaY);
                }

                break;
        }
        return true;
    }
}
